package com.dhcc.bpm.alarm.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.alarm.entity.AlarmNotice;
import com.dhcc.bpm.alarm.entity.AlarmNoticeTarget;
import com.dhcc.bpm.alarm.service.IAlarmNoticeInfoService;
import com.dhcc.bpm.alarm.service.IAlarmNoticeTargetService;
import com.dhcc.bpm.alarm.vo.AlarmNoticeVO;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 控制器
 *
 * @author xiafayu
 * @since 2022-02-16
 */
@RestController
@AllArgsConstructor
@NoArgsConstructor
@RequestMapping("alarm/alarmNotice")
@Api(value = "预警表", tags = "接口")
public class AlarmNoticeInfoController extends BladeController {
    @Autowired
    private IAlarmNoticeInfoService alarmNoticeService;
    @Autowired
    private ISerialNumberService serialNumberService;
    @Autowired
    private IAlarmNoticeTargetService alarmNoticeTargetService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入alarmNotice")
    public R<AlarmNoticeVO> detail(AlarmNotice alarmNotice) {
        AlarmNotice detail = alarmNoticeService.getOne(Condition.getQueryWrapper(alarmNotice));
        AlarmNoticeVO returnDetail = new AlarmNoticeVO();
        BeanUtil.copyProperties(detail, returnDetail);
        QueryWrapper<AlarmNoticeTarget>  queryWrapper = new QueryWrapper<AlarmNoticeTarget>();
        queryWrapper.eq("notice_id", detail.getId());
        List<AlarmNoticeTarget> noticeTargetList = alarmNoticeTargetService.list(queryWrapper);
       
        String targetId = noticeTargetList.stream().map(AlarmNoticeTarget::getTargetId).map(tid -> tid+"").collect(Collectors.joining(","));
        
        returnDetail.setTargetId(targetId);
        return R.data(returnDetail);
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入alarmNotice")
    public R<IPage<AlarmNotice>> list(AlarmNotice alarmNotice, Query query) {
        IPage<AlarmNotice> pages = alarmNoticeService.page(Condition.getPage(query), Condition.getQueryWrapper(alarmNotice));
        return R.data(pages);
    }

    /**
     * 自定义分页
     */
    @GetMapping("/qryList")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入alarmNotice")
    public R<IPage<AlarmNoticeVO>> page(AlarmNoticeVO alarmNotice, Query query) {
        IPage<AlarmNoticeVO> pages = alarmNoticeService.selectAlarmNoticePage(Condition.getPage(query), alarmNotice);
        return R.data(pages);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入alarmNotice")
    public R save(@Valid @RequestBody AlarmNotice alarmNotice) {
        return R.status(alarmNoticeService.save(alarmNotice));
    }

    /**
     * 修改
     */
    @PostMapping("/edit")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入alarmNotice")
    public R update(@Valid @RequestBody AlarmNotice alarmNotice) {
        return R.status(alarmNoticeService.updateById(alarmNotice));
    }
    
    /**
     * 新增或修改
     */
//    @PostMapping("/submit")
//    @ApiOperationSupport(order = 6)
//    @ApiOperation(value = "新增或修改", notes = "传入alarmNotice")
//    public R submit(@Valid @RequestBody AlarmNotice alarmNotice) {
//        if (alarmNotice.getId() == null) {
//            String noticeCode = serialNumberService.generateSerialNumberByModelCode("notice_code");
//            alarmNotice.setNoticeCode(noticeCode);
//        }
//        return R.status(alarmNoticeService.saveOrUpdate(alarmNotice));
//    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入alarmNotice,alarmNoticeTargetList")
    public R submit(@Valid @RequestBody JSONObject request) {
    	
    	AlarmNotice alarmNotice = request.getObject("alarmNotice", AlarmNotice.class);
    	
    	List<AlarmNoticeTarget> alarmNoticeTargetList = null;
    	if(request.containsKey("alarmNoticeTargetList")) {
    		alarmNoticeTargetList = JSONObject.parseArray(request.getJSONArray("alarmNoticeTargetList").toString(), AlarmNoticeTarget.class);
    	}
    	
    	if (alarmNotice.getId() == null) {
            String noticeCode = serialNumberService.generateSerialNumberByModelCode("notice_code");
            alarmNotice.setNoticeCode(noticeCode);
        }
        
        return R.status(alarmNoticeService.saveOrUpdate(alarmNotice, alarmNoticeTargetList));
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
    	
    	if(StringUtils.isNotBlank(ids)) {
    		QueryWrapper<AlarmNoticeTarget>  deleteWrapper = new QueryWrapper<AlarmNoticeTarget>();
        	deleteWrapper.in("notice_id", Func.toLongList(ids));
        	alarmNoticeTargetService.remove(deleteWrapper);
    	}
    	
        return R.status(alarmNoticeService.removeByIds(Func.toLongList(ids)));
    }

    /**
     * 新增或修改
     */
    @PostMapping("/changeStatus")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "修改预警管理状态", notes = "传入alarmNotice")
    public R changeStatus(@Valid @RequestBody AlarmNotice alarmNotice) {
        return R.status(alarmNoticeService.saveOrUpdate(alarmNotice));
    }
}
